package com.jzr.open.config.multidb;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
public class DbTransactionConfig implements TransactionManagementConfigurer {

    @Resource(name="db1TxManager")
    private PlatformTransactionManager db1TxManager;

    @Bean(name = "db1TxManager")
    @Primary
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1") DataSource db1) {
        return new DataSourceTransactionManager(db1);
    }

//    @Bean(name = "db2TxManager")
//    public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2") DataSource db2) {
//        return new DataSourceTransactionManager(db2);
//    }

    // 实现接口 TransactionManagementConfigurer 方法，其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return db1TxManager;
    }
}
